package com.hongxin.nw.netty.down;

import com.hongxin.nw.netty.model.Transfer;
import com.hongxin.nw.netty.model.packing.ASDUcsdna1;
import com.hongxin.nw.netty.model.packing.info.Csdna1_12;
import com.hongxin.nw.netty.utils.FieldPack;
import com.hongxin.nw.netty.utils.pools.AsduStartPools;
import com.hongxin.nw.netty.utils.pools.CodeTypePools;
import com.hongxin.nw.netty.values.StaticValue;
import com.hongxin.nw.netty.values.TypeIdentif;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @remark 远程启动充电(临时计费策略版本)
 * @author: GuoXiaolin
 * @date 2019/6/25 15:24
 * @Version 1.0
 * @Descibe
 */
@Slf4j
public class TempStartChargeReq implements IChannelRequest {
    @SuppressWarnings("unchecked")
    @Override
    public String doPacking(Transfer transfer) throws Exception {

        String code = transfer.getCode();
        Map<String, Object> map = (Map<String, Object>) transfer.getObj();
        String bj = String.valueOf(map.get("bj"));
        String money = String.valueOf(map.get("money"));
        String uid = String.valueOf(map.get("uid"));
        String cType = String.valueOf(map.get("cType"));
        String cParam = String.valueOf(map.get("cParam"));
        String json = String.valueOf(map.get("data"));

        Csdna1_12 csdna1_12 = new Csdna1_12()
                .setCo(code)
                .setBj(bj)
                .setYe(Double.valueOf(money))
                .setUid(uid)
                .setCType(cType)
                .setCParam(Double.valueOf(cParam));

        //********下发计费模型下行数据,临时奇葩计费策略********************
        Transfer transfer1 = new Transfer(code, null, json,
                TypeIdentif.M_DEFAULT.getInt(), CodeTypePools.getCodeType(code),
                Transfer.noNeedRecord, Transfer.noNeedMq, null);

        BillingReq2 billingReq = new BillingReq2();
        String bb = billingReq.doPacking(transfer1);
        //**********************************************************

        //TYPE = 133 , COT = 45
        ASDUcsdna1 mm = new ASDUcsdna1();
        mm.setLogType(String.valueOf(TypeIdentif.RECORD_REMOTE_START_CHARGE.getInt()));
        mm.setSendData(csdna1_12.packing() + StaticValue.JF + bb);

//        as.setType(TypeIdentif.IDENTIFY_C_SD_NA_1.getInt());
//        as.setCot(TypeIdentif.COT_UNKNOWN_COT.getInt());
//        as.setStaadr(AsduStartPools.getAsduStart(code).getStatAddress());
//        as.setInfoadr(Integer.valueOf(bj));
//        as.setVsq(StaticValue.DEFAULT_VSQ, StaticValue.SQ0);


        String as = FieldPack.packASDU(TypeIdentif.IDENTIFY_C_SD_NA_1.getInt(),
                TypeIdentif.COT_UNKNOWN_COT.getInt(),
                AsduStartPools.getAsduStart(code).getStatAddress(), Integer.valueOf(bj),
                StaticValue.DEFAULT_VSQ, StaticValue.SQ0);

        String data = as + mm.getHexData();

//        APCI ac = new APCI();
//        ac.setIdentifyStart(TypeIdentif.IDENTIFY_START.getInt());
//        ac.setApduLength(as.getHexData().length()/2);
//        ac.setContarea(TypeIdentif.FORMAT_I.getStr(),StaticValue.U_TEST_V);
//
//        return ac.getHexData() + as.getHexData();

        String ac = FieldPack.packAPCI(data.length(), TypeIdentif.FORMAT_I.getStr(), StaticValue.U_TEST_V);
        return ac + data;

    }
}
